package com.itaming.lycheeframework.mybatisplus.base;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itaming.lycheeframework.support.utils.ObjectUtil;

import java.io.Serializable;
import java.util.function.Supplier;

/**
 * 基础服务实现类
 *
 * @author A.Ming
 */
public class BaseServiceImpl<M extends BaseMapper<T>, T> extends ServiceImpl<M, T> implements BaseService<T> {

    /**
     * 根据<code>ID</code>查询
     *
     * @param id              ID
     * @param exceptionIfNull 查询结果为空时的异常
     * @return 查询结果
     */
    @Override
    public T getById(Serializable id, Supplier<? extends RuntimeException> exceptionIfNull) {
        return ObjectUtil.throwIfNull(super.getById(id), exceptionIfNull);
    }

    /**
     * 根据条件查询
     *
     * @param queryWrapper    查询条件
     * @param exceptionIfNull 查询结果为空时的异常
     * @return 查询结果
     */
    @Override
    public T getOne(Wrapper<T> queryWrapper, Supplier<? extends RuntimeException> exceptionIfNull) {
        return ObjectUtil.throwIfNull(super.getOne(queryWrapper), exceptionIfNull);
    }

}
